-- clear_one_feature
-- create by huanghr
-- 技能：清除特性

return {
    apply = function(source, target, classId, value)
        -- 不是怪物不管
        if target.type ~= OBJECT_TYPE_MONSTER then
            return;
        end

        -- 怪物已死亡不管
        if target:isDead() then
            return;
        end

         -- 抛出使用道具攻击事件
        EventMgr.fire(event.PROPERTY_ATTACK, { ["classId"] = classId, ["target"] = target, ["source"] = source});

        -- 移除怪物的某一条属性
        local skillIds = target:getAllSkills() or {};
        local skillAmount = #skillIds;

        if skillAmount < 1 then
            return;
        end

        --
        local dbase;
        local forbidClear;
        local id;
        local seed = DungeonM.getRandSeed("clear_one_feature");
        if skillAmount > 1 then
            -- 先排序再移除
            local function sortId(id1, id2)
                return id1 < id2;
            end

            table.sort(skillIds, sortId)

            printTable(skillIds, "clear_one_feature");

            local index = seed % skillAmount + 1;
            id = skillIds[index];

            local i = 1;

            dbase = SkillM.query(id, "dbase") or {};

            forbidClear = dbase["forbid_clear"] or 0;

            -- 没有合适的从头到尾找一遍
            while forbidClear == 1 and i <= skillAmount do
                id = skillIds[i];

                dbase = SkillM.query(id, "dbase") or {};

                forbidClear = dbase["forbid_clear"] or 0;

                index = i;
                i = i + 1;
            end

            -- 还没有没找到合适的，返回
            if index > skillAmount then
                return;
            end

            id = skillIds[index];

            table.remove(skillIds, index);
        else
            id = skillIds[1];
            dbase = SkillM.query(id, "dbase") or {};

            forbidClear = dbase["forbid_clear"] or 0;

            if forbidClear == 1 then
                return;
            else
                table.remove(skillIds, 1);
            end
        end

        printTable(target:getAllSkills(), "target:getAllSkills");

        PropM.refreshEffect(target, "skill");
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 不需要解析
        return desc;
    end,
};
